<template>
  <div>
    <el-form :inline="true" :model="dataForm" ref="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="用户账号">
        <el-input v-model="dataForm.account" placeholder="用户账号" size="small"
             style="width: 150px" clearable></el-input>
      </el-form-item>
       <el-form-item label="用户名称">
         <el-input v-model="dataForm.userName" placeholder="用户名称" size="small"
              style="width: 150px" clearable></el-input>
       </el-form-item>
       <el-form-item label="手机号">
         <el-input v-model="dataForm.phone" placeholder="手机号" size="small"
              style="width: 150px" clearable></el-input>
       </el-form-item>
       <el-form-item label="邮箱">
         <el-input v-model="dataForm.email" placeholder="邮箱" size="small"
              style="width: 150px" clearable></el-input>
       </el-form-item>
       <el-form-item>
         <el-button type="primary_new" icon="el-icon-search" size="mini" plain @click="getDataList()">搜索</el-button>
         <el-button icon="el-icon-refresh" size="mini" plain @click="resetQuery()">重置</el-button>
       </el-form-item>
     </el-form>

     <el-row :gutter="10" class="mb8">
       <el-col :span="1.5">
         <el-button type="primary_new" plain icon="el-icon-plus" size="mini" @click="handleAdd()"  @refreshDataList="getDataList">新增</el-button>
       </el-col>
       <el-col :span="1.5">
         <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>
       </el-col>
       <el-col :span="1.5">
         <upload-file v-if="uploadFileVisible" ref="uploadImage" @refreshDataList="getDataList"></upload-file>
         <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" >导入</el-button>
       </el-col>
       <el-col :span="1.5">
         <el-button type="warning" plain icon="el-icon-download" size="mini"  @click="handleExport()" >导出</el-button>
       </el-col>
       <el-col :span="1.5">
         <el-button type="success"plain icon="el-icon-download" size="mini" @click="handleDemoExport()" >示例导出</el-button>
       </el-col>
     </el-row>
     <div style="height: 20px;"></div>
 <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      :default-sort = "{order: 'descending'}"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        type="index"
        fixed="left"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="account"
        header-align="center"
        fixed="left"
        align="center"
        label="账号">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="gender"
        header-align="center"
        align="center"
        label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender === 1" size="small" type="danger">男</el-tag>
          <el-tag v-else size="small">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        width="140px"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        width="150px"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        width="100"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        width="100"
        label="更新人">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        sortable
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        width="180"
        sortable
        label="更新时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="success" size="small" plain @click="updateHandle(scope.row.id)">修改</el-button>
          <el-button type="danger" size="small" plain @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[7, 14, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <student-Info-add-update v-if="addOrUpdateVisible" ref="handleAdd" @refreshDataList="getDataList"></student-Info-add-update>
    <student-Info-update v-if="addOrUpdateVisible" ref="updateHandle" @refreshDataList="getDataList"></student-Info-update>

  </div>
</template>

<script>
   /* eslint-disable */
   import UploadFile from '../../compont/upload-file.vue'
   import StudentInfoAddUpdate from './student-Info-add-update.vue'
   import StudentInfoUpdate from './student-Info-update.vue'


  export default{
    components: {UploadFile,StudentInfoAddUpdate,StudentInfoUpdate},
    data () {
      return {
        dataForm: {
          userName: '',
          phone: '',
          email: '',
          account:'',
        },
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        dataList: [],
        pageIndex: 1,
        pageSize: 7,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        uploadFileVisible: false,
      }
    },
    created() {

    },
    activated () {
      this.getDataList()
    },
    methods:{
      resetQuery () {
        this.dataForm ={
          userName: '',
          phone: '',
          email: '',
          account:'',
        }
        this.getDataList()
      },
      handleAdd () {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.handleAdd.init()
        })
      },
      handleDemoExport () {
        window.location.href = 'http://qpwg6j2tf.hd-bkt.clouddn.com/%E7%AE%A1%E7%90%86%E5%91%98%E4%BF%A1%E6%81%AF.xlsx'
      },
      handleImport (type) {
        type == 3
         this.uploadFileVisible = true
         this.$nextTick(() => {
           this.$refs.uploadImage.init(type)
         })
      },
      handleExport () {
        window.location.href = 'http://qpwg6j2tf.hd-bkt.clouddn.com/%E7%AE%A1%E7%90%86%E5%91%98%E4%BF%A1%E6%81%AF.xlsx'
      },
      updateHandle (id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.updateHandle.init(id)
        })
      },
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/student/page'),
          method: 'post',
          data: this.$http.adornData({
            'account': this.dataForm.account,
            'userName': this.dataForm.userName,
            'phone': this.dataForm.phone,
            'email': this.dataForm.email,
            'current': this.pageIndex,
            'size': this.pageSize
          })
        }).then(({data}) => {
          if (data.success) {
            console.log("data.data.rows:",data.data.rows)
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.message)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      deleteHandle (id) {
          var userIds = id ? [id] : this.dataListSelections.map(item => {
            return item.id
          })
          this.$confirm(`确定要进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$http({
              url: this.$http.adornUrl('/student/delete'),
              method: 'post',
              data: this.$http.adornData({
                'ids': userIds
              })
            }).then(({data}) => {
              if (data.success) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.pageIndex = 1
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }).catch(() => {})
        }
    }
  }
</script>

<style>
  .el-button--primary_new.is-plain {
      color: #17B3A3;
      background: rgb(232, 247, 246);
      border-color: rgb(162, 225, 218);
  }
</style>
